<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $components = array(
			'Session',
			'Auth' => array(
					'logoutRedirect' => '/',
					'loginAction' => array(
							'controller' => 'users',
							'action' => 'login'
					),
					'authError' => 'Opera&ccedil;&atilde;o n&atilde;o permitida !!!',
					'authenticate' => 'Form'));
	
	public $usuarioAtual;
	public $logado;
	
	public function isAuthorized($usuario){
		return  true;
	}
	
	function beforeFilter(){
		$this->logado = $this->Auth->loggedIn();
		$this->usuarioAtual = $this->Auth->user();
		//$this->set('logado', $this->Auth->loggedIn());
		$this->set('usuarioAtual', $this->usuarioAtual);
		$this->set('logado', $this->logado);
		$this->Auth->deny(); //Arranca essa porra!!!
	}
	
	protected function isAdmin(){
		if($this->logado && $this->usuarioAtual['user_type'] == 0)
			return true;
		else return false;
	}
	
	protected function isUser(){
		if($this->logado && ($this->usuarioAtual['user_type'] == 2 || $this->isRevisor()))
			return true;
		else return false;
	}
	
	protected function isRevisor(){
		if($this->logado && $this->usuarioAtual['user_type'] == 1)
			return true;
		else return false;
	}
	
}
